<?php 
require 'config.php';
$css_arr = array(
	'css/home/home.css',
	'css/sidebar-noticias.css',
  'css/calendar.css'
);
$js_arr = array('js/home/home.js');
if (LANG == 'en')
 array_push($css_arr, 'css/home/home-en.css', 'css/sidebar-noticias-en.css');
require_once PATH . 'layout/header.php';
Lang::setSource('home');

// Trae los banners
$query = "SELECT * FROM `banner` ORDER BY `orden` ASC";
$banner_result = mysql_query($query, $conexion) or die('Error al leer los banners. '.mysql_error());

// Cargar lista de eventos desde la BD
$query = 'SELECT e.*'; 
$query .= 'FROM evento e LEFT JOIN fecha f ON e.id = f.evento_id ';
$query .= 'WHERE estado = "n" ';
$query .= 'GROUP BY e.id ORDER BY fecha ASC LIMIT 0, 6';
$evento_result = mysql_query($query)
  or die('Error al traer los eventos. ' . mysql_error());
$evento = mysql_fetch_assoc($evento_result);
$eventos_nuevos_total = mysql_affected_rows();
?>
    <!-- HOME -->
    <div id="home_banners_bullets"></div>
    <section id="concerts" class="clearfix">
      <div class="clearfix">
        <h1><?php echo Lang::get('title') ?></h1>
        <a href="<?php echo BASE ?>conciertos-eventos/" id="all_concerts_btn"></a>
      </div>
      
      <div class="clearfix">
        <?php
        $eventos_count = 0;
        $article_num = 1;

        if( $eventos_nuevos_total )
        do {
        	$desc_arr = array();
          $eventos_count++;
          $third_class = ($eventos_count % 3 == 0) ? 'third' : '';
          $title = $evento['titulo' . (LANG == 'en' ? '_en' : '')];
          $program = $evento['programa' . (LANG == 'en' ? '_en' : '')];
          $description = $evento['descripcion' . (LANG == 'en' ? '_en' : '')];
          $desc_arr = explode('<!-- pagebreak -->', $description);
          $description = $desc_arr[0];

          if ($article_num == 1 || $article_num == 4) {
            echo '<div class="clearfix">';
          }
        ?>
        <article class="concert <?php echo $third_class ?>">
          <a href="<?php echo BASE ?>conciertos-eventos/evento/?id=<?php echo $evento['id'] ?>">
          	<img src="img/uploads/events/202x95/<?php echo $evento['id'] ?>.jpg" alt="<?php echo $program; ?> - OFJ" title="<?php echo $program; ?> - OFJ" border="0" width="202" height="95" />
          </a>
          <?php
          // Traer temporada
          $query_res = mysql_query('SELECT `nombre' . (LANG == 'en' ? '_en' : '') .'` AS `nombre` FROM `temporada` WHERE `id`=' . $evento['temporada_id']);
          $temporada = mysql_fetch_assoc($query_res);
          ?>
          <div class="clearfix">
	          <span class="concert_season"><?php echo $temporada['nombre'] ?></span>
	          <span class="arrow"></span>
          </div>
          <span class="concert_program"><?php echo $program; ?></span>
          
          <h2 class="concert_title"><?php echo $title; ?></h2>
          <?php
          // Traer fechas
          $query_res = mysql_query('SELECT `fecha` FROM `fecha` WHERE `evento_id`=' . $evento['id']);
          while( $fecha = mysql_fetch_assoc($query_res)) :
          	if (LANG == 'en')
          	 $date = strftime('%A, %B %d, %Y', strtotime( $fecha['fecha'] ));
          	else
          	$date = strftime('%A %d de %B del %Y', strtotime( $fecha['fecha'] ));
          ?>
          <div class="concert_date"><?php echo $date ?></div>
          <?php endwhile /* Listar fechas */ ?>
          
          <div class="concert_info">
            <?php echo $description; ?>
          </div>

          <?php
          // Traer sede
          $query_res = mysql_query('SELECT `nombre` FROM `sede` WHERE `id`=' . $evento['sede_id']);
          $sede = mysql_fetch_assoc($query_res);
          ?>
          <div class="concert_place"><?php echo $sede['nombre'] ?></div>
          
          <a href="<?php echo BASE ?>conciertos-eventos/evento/?id=<?php echo $evento['id'] ?>" class="event_details"><?php echo LANG::get('event_details') ?></a>
        </article>
        <?php  
            if ($article_num == 3 || $article_num == 6) {
              echo '</div>';
            }

            $article_num++;
          } while($evento = mysql_fetch_assoc($evento_result)); /* Listar eventos */ 
        ?>



        <?php
        /**
         * EVENTOS CONSUMADOS
         */
        if ( $eventos_nuevos_total < 6) {
          $limit = 6 - $eventos_nuevos_total;

          $query = 'SELECT e.*'; 
          $query .= 'FROM evento e LEFT JOIN fecha f ON e.id = f.evento_id ';
          $query .= 'WHERE estado = "c" ';
          $query .= "GROUP BY e.id ORDER BY fecha ASC LIMIT 0, $limit";
          $evento_result = mysql_query($query)
            or die('Error al traer los eventos. ' . mysql_error());
          $evento = mysql_fetch_assoc($evento_result);
        

          do {
            $desc_arr = array();
            $eventos_count++;
            $third_class = ($eventos_count % 3 == 0) ? 'third' : '';
            $title = $evento['titulo' . (LANG == 'en' ? '_en' : '')];
            $program = $evento['programa' . (LANG == 'en' ? '_en' : '')];
            $description = $evento['descripcion' . (LANG == 'en' ? '_en' : '')];
            $desc_arr = explode('<!-- pagebreak -->', $description);
            $description = $desc_arr[0];
        ?>
        <article class="concert <?php echo $third_class ?>">
          <a href="<?php echo BASE ?>conciertos-eventos/evento/?id=<?php echo $evento['id'] ?>">
            <img src="img/uploads/events/202x95/<?php echo $evento['id'] ?>.jpg" alt="<?php echo $program; ?> - OFJ" title="<?php echo $program; ?> - OFJ" border="0" width="202" height="95" />
          </a>
          <?php
          // Traer temporada
          $query_res = mysql_query('SELECT `nombre' . (LANG == 'en' ? '_en' : '') .'` AS `nombre` FROM `temporada` WHERE `id`=' . $evento['temporada_id']);
          $temporada = mysql_fetch_assoc($query_res);
          ?>
          <div class="clearfix">
            <span class="concert_season"><?php echo $temporada['nombre'] ?></span>
            <span class="arrow"></span>
          </div>
          <span class="concert_program"><?php echo $program; ?></span>
          
          <h2 class="concert_title"><?php echo $title; ?></h2>
          <?php
          // Traer fechas
          $query_res = mysql_query('SELECT `fecha` FROM `fecha` WHERE `evento_id`=' . $evento['id']);
          while( $fecha = mysql_fetch_assoc($query_res)) :
            if (LANG == 'en')
             $date = strftime('%A, %B %d, %Y', strtotime( $fecha['fecha'] ));
            else
            $date = strftime('%A %d de %B del %Y', strtotime( $fecha['fecha'] ));
          ?>
          <div class="concert_date"><?php echo $date ?></div>
          <?php endwhile /* Listar fechas */ ?>
          
          <div class="concert_info">
            <?php echo $description; ?>
          </div>

          <?php
          // Traer sede
          $query_res = mysql_query('SELECT `nombre` FROM `sede` WHERE `id`=' . $evento['sede_id']);
          $sede = mysql_fetch_assoc($query_res);
          ?>
          <div class="concert_place"><?php echo $sede['nombre'] ?></div>
          
          <a href="<?php echo BASE ?>conciertos-eventos/evento/?id=<?php echo $evento['id'] ?>" class="event_details"><?php echo LANG::get('event_details') ?></a>
        </article>
        <?php 
          } while($evento = mysql_fetch_assoc($evento_result)); /* Listar eventos */ 
        } // end if ( $eventos_nuevos_total < 6)
        ?>
      </div>
    </section> <!-- end of #concerts -->
    
    <?php
      require PATH . 'sidebar-noticias.php'; 
    ?>
    <!-- END HOME -->

<script type="text/javascript">
  var _banners = [];
  <?php while( $banner = mysql_fetch_assoc($banner_result) ) { ?>
    _banners.push( <?php echo json_encode($banner) ?> );
  <?php } ?>
</script>
<?php 
require_once PATH . 'layout/footer.php';
mysql_free_result($banner_result);
mysql_close($conexion);
?>
